package cn.jsj.invite.controller;

import cn.jsj.invite.interceptor.RequestThreadContext;
import cn.jsj.invite.interceptor.swich.PassToken;
import cn.jsj.invite.pojo.InvitationInfo;
import cn.jsj.invite.pojo.SchoolInfo;

import cn.jsj.invite.pojo.StudentEnt;
import cn.jsj.invite.result.Result;
import cn.jsj.invite.service.SchoolService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class SchoolController {

    private final SchoolService schoolService;

    public SchoolController(SchoolService schoolService) {
        this.schoolService = schoolService;
    }

    //查看学校简单信息
    @PassToken
    @GetMapping("/findSchoolSimpleInfo")
    public Result<SchoolInfo> findSchoolSimpleInfo() {
        return Result.success(schoolService.listSchoolSimpleInfo());
    }

    //学校审核帖子通过
    @PostMapping("/reviewPost/{postId}")
    public Result<?> reviewPost(@PathVariable Integer postId, @RequestParam Integer schoolId, @RequestParam Integer schoolAdminId) {
        Integer integer = schoolService.reviewInvitation(schoolId, schoolAdminId, postId);
        return propertyOk(integer);
    }

    //学校审核学生注册信息
    @PostMapping("/review/student/register/{stuId}")
    public Result<?> reviewStudentRegister(@PathVariable Integer stuId, Integer schoolId, Integer schoolAdminId) {
        Integer integer = schoolService.reviewStudentRegister(schoolId, schoolAdminId, stuId);
        return propertyOk(integer);
    }

    //学校查看所有待审核的帖子
    @GetMapping("/findPostReviewed")
    public Result<InvitationInfo> findPostReviewed(@RequestParam Integer schoolId) {
        Long schoolAdminId = RequestThreadContext.requesterUserId();
        List<InvitationInfo> invitationInfos = schoolService.listInvitationNeedApprove(schoolId, schoolAdminId);
        return Result.success(invitationInfos);
    }

    //学校根据姓名模糊查询学生
    @GetMapping("/school/findStudentByName/{schoolId}/{stuName}")
    public Result<StudentEnt> findStudentByName(@PathVariable Integer schoolId, @PathVariable String stuName) {
        return Result.success(schoolService.listStudentsByName(schoolId, stuName));
    }

    //学校根据学号查询某个学生
    @GetMapping("/school/findStudentByNumber/{schoolId}/{stuNumber}")
    public Result<StudentEnt> findStudentByNumber(@PathVariable Integer schoolId, @PathVariable String stuNumber) {
        return Result.success(schoolService.getOneStudentByNumber(schoolId, stuNumber));
    }

    //学校根据院系查询学生
    @GetMapping("/school/findStudentByFaculty/{schoolId}/{faculty}")
    public Result<StudentEnt> findStudentByFaculty(@PathVariable Integer schoolId, @PathVariable String faculty) {
        return Result.success(schoolService.listStudentsByFaculty(schoolId, faculty));
    }

    /**
     * 管理员删除帖子
     *
     * @param pageId 帖子ID
     * @return 1；成功 0：失败
     */
    @GetMapping("/deletePageById/{pageId}")
    public Result<String> deletePageByPageId(@PathVariable Integer pageId, Integer schoolId, Integer schoolAdminId) {
        if (schoolService.deletePageByPageId(pageId, schoolId, schoolAdminId).equals(1)) {
            return Result.success("1");
        } else {
            return Result.error("0");
        }
    }

    /**
     * 管理员修改密码 通过旧密码以及adminID
     *
     * @param adminOldPassword 旧密码
     * @param adminNewPassword 新密码
     * @param adminId          管理员ID
     * @return 1：成功 0：失败
     */
    @PostMapping("/school/updatePassword")
    public Result<String> modifyAdminPassword(String adminOldPassword, String adminNewPassword, Integer adminId) {
        if (schoolService.updatePasswordByOldPwdAndAdminId(adminOldPassword, adminNewPassword, adminId).equals(1)) {
            return Result.success("1");
        } else {
            return Result.error("0");
        }
    }

    //修改学校管理员的联系方式
    @PostMapping("/contact/update")
    public Result<?> contactUpdate(Integer adminId, String phone) {
        if (adminId == null || phone == null) {
            Result<Object> result = Result.fail("数据为空");
            result.setStatus(0);
            return result;
        }
        if (schoolService.updateSchoolAdminConcat(adminId, phone).equals(1)) {
            return Result.success();
        }
        Result<Object> result = Result.fail("手机号格式有误");
        result.setStatus(0);
        return result;
    }

    //学校查看待审核的学生
    @PostMapping("/student/reviewed/list")
    public Result<?> studentReviewedList(Integer adminId, Integer schoolId) {
        List<StudentEnt> studentEnts = schoolService.listStudentNeedApprove(adminId, schoolId);

        if (studentEnts.size() != 0) {
            return Result.success(studentEnts);
        }
        return Result.fail("查无信息");
    }

    //学校禁用学生
    @PostMapping("/disable/student")
    public Result<?> disableStudent(Integer stuId) {
        Integer integer = schoolService.disableStudentById(stuId);
        if (integer == null) {
            return Result.fail("参数缺失");
        }
        if (integer.equals(1)) {
            return Result.success();
        }
        return Result.fail("禁用失败");
    }

    //学校管理员直接修改学生密码
    @PostMapping("/school/update/stuPassword")
    public Result<?> schoolUpdateStudentPassword(Integer stuId, String newPassword) {
        Integer integer = schoolService.schoolUpdatePassword(stuId, newPassword);
        if (integer == null) {
            return Result.fail("参数为空");
        }
        if (integer.equals(1)) {
            return Result.success();
        }
        return Result.fail("修改失败");
    }

    //学校查看企业发布的帖子
    @PostMapping("/school/findAll/enterprise")
    public Result<?> schoolFindAllEnterprise(Integer schoolId) {
        List<InvitationInfo> invitationInfos = schoolService.schoolFindAllEnterprise(schoolId);
        if (invitationInfos == null) {
            return Result.fail("参数有误");
        }
        return Result.success(invitationInfos);
    }

    //判断业务层返回参数有效性，返回不同的结果提示
    private Result<?> propertyOk(Integer integer) {
        if (integer == null) {
            return Result.fail("参数不完整");
        }
        if (integer.equals(-1)) {
            return Result.fail("参数无效");
        }
        if (integer.equals(1)) {
            return Result.success();
        }
        return Result.fail("操作失败");
    }

}
